<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<script src="./vue.js"></script>
<script src="./vuex.js"></script>

<body>
  <div id="app">
    <button @click="asyc">异步操作</button>
    <p> {{ this.$store.state.count }}</p>
  </div>

  <script>
    const store = new Vuex.Store({
      state: {
        count: 0,
      },
      mutations: {
        receive(state) {
          state.count++;
          // 真实情况并不推荐在同步mutations添加异步操作
          // setTimeout(() => {
          //   state.count++;
          // }, 2000)
          let i = setInterval(() => {
            state.count++;
            if (state.count === 10) {
              clearInterval(i);
            }
          }, 1000)
        }
      }
    })

    const vm = new Vue({
      el: "#app",
      store,
      methods: {
        asyc() {
          this.$store.commit("receive")
        }
      }
    })
  </script>
</body>

</html>